Unit Testing

We'll start by writing a test for a very simple Stream Processor. The Stream Processor will be a jsDataStreamCreator which ignores the incoming payload and returns the result of the jsFunc. Here is the text for the jsFunc:

return [ new code_data_SimpleData("hello, world!") ];

This jsFunc (also pictured below) creates a SimpleData object containing the string "hello, world!".

You'll notice that at the bottom of the Stream Processor's editor, you see the following:

The "+ Tests" is a "button" you can click to show the tests for the Stream Processor. What you don't see next to the "Remove Stream Processor" button is the "Test" button which will appear when the Tests are minimally configured. More on that, shortly.

Click the "+ Tests" button and you will see the following:

To make your test runnable, the minimum configuration is filling in the "testDataTransformFunc" field. When you fill in the "testDataTransformFunc" field, and save the Orchestration, the "Test" button will appear next to the "Remove Stream Processor" button.

These three fields, testData, testDataTransformFunc, and assertionFunc will be introduced one by one over the course of this document. For now let's just enter the following in the "testDataTransformFunc" field and then save the Orchestration:

return [];

It should look like this:

Press the Test button that has appeared to the left of the Remove Stream Processor button.

A window titled Test Result will pop up. It has a lot of information in it so we'll chop up the window and look at it slice by slice.

The true text means that the test passed! If a test runs wihtout any errors and has no assertionFunc configured it is guaranteed to pass. If the test had failed for some reason, you'd see the word false instead.

The next slice shows the input payload:

This input payload looks almost blank; empty arrays and objects. That's okay because we are testing a jsDataStreamCreator Stream Processor which completely ignores its input anyway.

The above image has been cut and spliced to just show the relevant information. This is a JSON representation of the payload which came out of our jsDataStreamCreator. The most interesting part of the output is the highlighted text. It should look familiar because it's what our jsDataStreamCreator Stream Processor returned in its jsFunc.

A test without assertions isn't very useful, so let's add an assertion, but let's make sure the test fails.

Enter the following into the assertionFunc field:

return false;

It should look like this:

What this means is that the assertionFunc is a predicate that returns true if the test passes and false if the test fails.

Save the Orchestration and press the Test button.

This time you should see the following at the top of the Test Result window:

Let's make the test pass. We could just change the false to true, but that wouldn't be a very good test. We want our test to examine what came out of the jsDataStreamCreator Stream Processor and only pass if the output matches our expectation.

To do that we need to know how to access the output of the Stream Processor. The output of a Stream Processor either contains an error or a Stream of results. Our tests will assume that the StreamProcessors don't return errors so we'll need the following code to get access to the Stream of results. No need to enter this code anywhere; we'll build the code incrementally at first.

var stream = payload.in().right().toOption().get();

The above code gives us a Stream of results, but we want to write our test against the first element in the stream since our jsDataStreamCreator created a Stream containing only one object. Given a stream JavaScript object named "stream" we can get its first result with this code:

var item = stream.head().right().toOption().get();

For brevity, we'll combine the two preceding code snippets into one:

var item = payload.in().right().toOption().get().head().right().toOption().get();

You can remember this sequence of calls with the following mnemonic:

Payload, I'm Ready To Go Home. Ready To Go.

Or something more creative involving Radioisotope Thermoelectric Generators...

Now that we have access to the object that our jsDataStreamCreator returned, we want to make some assertion about it. Let's assert that it is equal to "hello, world!"

Enter this into the assertion field:

  // get the stream's first element
  var item = payload.in().right().toOption().get().head().right().toOption().get();
  // pass the test if the first element is equal to "hello, world!"
  return item.value() == "hello, world!";

Save the Orchestration and press the Test button. You should see the test pass, and see the text true at the top fo the Test Result window.

We now have a test that makes sure that our Stream Processor emits a certain value.